var express = require('express');
var router = express.Router();
// step 1
/* GET home page. */
// router.get('/', function(req, res, next) {
//     a;
//     res.send({
//         message: 'hello world'
//     });
// });

// a;

// step 2
// router.get('/', async function(req, res, next) {
//     a;
//     res.send({
//         message: 'hello world'
//     });
// });

//獨立 controller
const errController = async function(req, res, next) {
    a;
    res.send({
        message: 'hello world'
    });
};

const normalController = async function(req, res, next) {
    res.send({
        message: 'hello world'
    });
};
//錯誤捕捉=>回傳500
const errCatch = (asyncFn) => {
    return (req, res, next) => {
        asyncFn(req, res, next).catch((err) => {
            res.status(500).send({
                message: 'Internal Server Error'
            });
        });
    }
};
 
// router.get('/', errController );

router.get('/', errCatch(errController) );
// http://localhost:3000/err/nocatch
router.get('/nocatch', errController);
// http://localhost:3000/err/normal
router.get('/normal', errCatch(normalController) );
module.exports = router;
